<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sound Machine Settings</title>
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="css/settings.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="close">Close</div>
<div id="settings-screen" class="container-fluid">
    <div class="row">
        <div class="col-xs-12">
            <h2>设置</h2>

            <p>Modifier keys for global shortcuts</p>
            <p>
                <input type="checkbox" class="global-shortcut" id="global-shortcut-ctrl"
                       data-modifier-key="ctrl"/>
                <label for="global-shortcut-ctrl">Ctrl</label>
            </p>

            <p>
                <input type="checkbox" class="global-shortcut" id="global-shortcut-shift"
                       data-modifier-key="shift"/>
                <label for="global-shortcut-shift">Shift</label>
            </p>

            <p>
                <input type="checkbox" class="global-shortcut" id="global-shortcut-alt"
                       data-modifier-key="alt"/>
                <label for="global-shortcut-alt">Alt</label>
            </p>
        </div>
    </div>
</div>

<script type="application/javascript" charset="utf-8">
const {ipcRenderer} = require('electron');
	var closeEl = document.querySelector('.close');

closeEl.addEventListener('click', function (e) {
    ipcRenderer.send('close-settings-window');
});
	
 var configuration = require('../configuration');

		var modifierCheckboxes = document.querySelectorAll('.global-shortcut');
	for (var i = 0; i < modifierCheckboxes.length; i++) {
    var  shortcutKeys= configuration.readSettings('shortcutKeys');
    var modifierKey = modifierCheckboxes[i].attributes['data-modifier-key'].value;
    modifierCheckboxes[i].checked = shortcutKeys.indexOf(modifierKey) !== -1;
    modifierCheckboxes[i].addEventListener('click', function (e) {
    	
        bindModifierCheckboxes(e);
    });
}

function bindModifierCheckboxes(e) {
  var shortcutKeys = configuration.readSettings('shortcutKeys');
    var modifierKey = e.target.attributes['data-modifier-key'].value;
    if (shortcutKeys.indexOf(modifierKey) !== -1) {
        var shortcutKeyIndex = shortcutKeys.indexOf(modifierKey);
        shortcutKeys.splice(shortcutKeyIndex, 1);
    }
    else {
        shortcutKeys.push(modifierKey);
    }

    configuration.saveSettings('shortcutKeys', shortcutKeys);
    ipcRenderer.send('set-global-shortcuts');
}
</script>
</body>
</html>
